package com.miyu.common.utils.commonUtils;

import com.miyu.common.utils.filter.exception.CosException;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;

/**
 * IP 处理工具类
 * @author miyu
 * @date 2022/12/26
 */
@Slf4j
public  class IpUtils {
    /** 当前IP离线数据库地址*/
    /** 使用当前jar包下此文件*/
//    static String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+ "ip2region.xdb";
    /**  1、创建 searcher 对象*/
    public static String search(String ip) {
        String region="";
        Searcher searcher = null;
        try {
            /** 校验文件是否存在*/
            searcher = Searcher.newWithFileOnly(ClassLoader.getSystemResource("ip2region.xdb").getPath());
        } catch (Exception e) {
            log.error("ip2region.xdb : 离线数据库不存在！");
        }
        // 2、查询
        try {
             region = searcher.search(ip);
            searcher.close();
        } catch (Exception e) {
            log.error(ip+"：查询失败");
        }
        return region;
    }
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip!=null|| ip.length() >0){
            String[]  ipStr =ip.split(",");
            ip=ipStr[0];
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getIpAddr() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        String ip = request.getHeader("x-forwarded-for");
        if(ip!=null|| ip.length() >0){
            String[]  ipStr =ip.split(",");
            ip=ipStr[0];
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip ==null || ip.length() ==0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    /**
     * ip 归属地址
     * @param region 区域
     * @return {@link String}
     */
    public static String IpAddress(String region){
        String address="";
        String[] ipAddre = region.split("\\|");
        /** 排除0取值*/
        address = Arrays.stream(ipAddre).filter(item->!item.equals("0")).map(Object::toString).reduce("", String::concat);
        return address;
    }

    /**
     * ip 归属地址
     * local 取值范围
     * 国家|区域|省份|城市|ISP
     * @param region 区域
     * @return {@link String}
     */
    public static String IpAddress(String region,Integer local) {
        Integer lt=0;
        String address="";
        String[] ipAddre = region.split("\\|");
         lt =ipAddre.length;
        if(lt>local){
            lt=local;
        }
        for (int i = 0; i < lt; i++) {
            if(!ipAddre[i].equals("0")){
                address+=ipAddre[i];
            }
        }
        return address;
    }


}
